package co.com.tye.presentation.backingBeans;

import co.com.tye.exceptions.*;
import co.com.tye.modelo.*;
import co.com.tye.modelo.dto.ProgramaDTO;
import co.com.tye.presentation.businessDelegate.*;
import co.com.tye.utilities.*;

import org.primefaces.component.calendar.*;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.component.selectonemenu.SelectOneMenu;
import org.primefaces.event.RowEditEvent;

import java.io.Serializable;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

/**
 * @author Zathura Code Generator http://code.google.com/p/zathura/
 *         www.zathuracode.org
 *
 */
@ManagedBean
@ViewScoped
public class ProgramaView implements Serializable {
	private static final long serialVersionUID = 1L;

	private InputText txtIsbn;
	private InputText txtNombrePrograma;

	private InputText txtIdfacultad_Facultad;
	private InputText txtIdprograma;

	private SelectOneMenu selectOneMenuFacultad;
	private List<Facultad> lasFacultades;
	private List<SelectItem> lasFacultadesItems;
	private String stringFacultad;

	private CommandButton btnSave;
	private CommandButton btnModify;
	private CommandButton btnDelete;
	private CommandButton btnClear;
	private List<ProgramaDTO> data;
	private ProgramaDTO selectedPrograma;
	private Programa entity;
	private boolean showDialog;
	@ManagedProperty(value = "#{BusinessDelegatorView}")
	private IBusinessDelegatorView businessDelegatorView;

	public ProgramaView() {
		super();
	}

	public void rowEventListener(RowEditEvent e) {
		try {
			ProgramaDTO programaDTO = (ProgramaDTO) e.getObject();

			if (txtIsbn == null) {
				txtIsbn = new InputText();
			}

			txtIsbn.setValue(programaDTO.getIsbn());

			if (txtNombrePrograma == null) {
				txtNombrePrograma = new InputText();
			}

			txtNombrePrograma.setValue(programaDTO.getNombrePrograma());

			if (txtIdfacultad_Facultad == null) {
				txtIdfacultad_Facultad = new InputText();
			}

			txtIdfacultad_Facultad.setValue(programaDTO
					.getIdfacultad_Facultad());

			if (txtIdprograma == null) {
				txtIdprograma = new InputText();
			}

			txtIdprograma.setValue(programaDTO.getIdprograma());

			Integer idprograma = FacesUtils.checkInteger(txtIdprograma);
			entity = businessDelegatorView.getPrograma(idprograma);

			action_modify();
		} catch (Exception ex) {
		}
	}

	public String action_new() {
		action_clear();
		selectedPrograma = null;
		setShowDialog(true);

		return "";
	}

	public String action_clear() {
		entity = null;
		selectedPrograma = null;

		if (txtIsbn != null) {
			txtIsbn.setValue(null);
			txtIsbn.setDisabled(false);
		}

		if (txtNombrePrograma != null) {
			txtNombrePrograma.setValue(null);
			txtNombrePrograma.setDisabled(false);
		}

		if (txtIdfacultad_Facultad != null) {
			txtIdfacultad_Facultad.setValue(null);
			txtIdfacultad_Facultad.setDisabled(false);
		}

		if (txtIdprograma != null) {
			txtIdprograma.setValue(null);
			txtIdprograma.setDisabled(false);
		}

		if (btnSave != null) {
			btnSave.setDisabled(false);
		}

		if (btnDelete != null) {
			btnDelete.setDisabled(false);
		}

		return "";
	}



	public String action_edit(ActionEvent evt) {
		selectedPrograma = (ProgramaDTO) (evt.getComponent().getAttributes().get("selectedPrograma"));
		
		txtIsbn.setValue(selectedPrograma.getIsbn());
		txtIsbn.setDisabled(false);
		txtNombrePrograma.setValue(selectedPrograma.getNombrePrograma());
		txtNombrePrograma.setDisabled(false);
		
		btnSave.setDisabled(false);
		setShowDialog(true);

		return "";
	}

	public String action_save() {
		try {
			if ((selectedPrograma == null) && (entity == null)) {
				action_create();
			} else {
				action_modify();
			}

			data = null;
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_create() {
		try {
			entity = new Programa();
			entity.setIdprograma(0);
			entity.setEstadoRegistro("activo");
			entity.setFechaCreacion(new Date());
			entity.setIsbn(FacesUtils.checkString(txtIsbn));
			entity.setNombrePrograma(FacesUtils.checkString(txtNombrePrograma));
			
			
			entity.setFacultad(businessDelegatorView.getFacultad(Integer.parseInt(stringFacultad)));
			
			
			businessDelegatorView.savePrograma(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);

			action_clear();
		} catch (Exception e) {
			entity = null;
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_modify() {
		try {
			if (entity == null) {
				Integer idprograma = new Integer(
						selectedPrograma.getIdprograma());
				entity = businessDelegatorView.getPrograma(idprograma);
			}
			entity.setIsbn(FacesUtils.checkString(txtIsbn));
			entity.setNombrePrograma(FacesUtils.checkString(txtNombrePrograma));
			entity.setFechaUltimaModificacion(new Date());
			entity.setFacultad(businessDelegatorView.getFacultad(Integer.parseInt(stringFacultad)));
			
			businessDelegatorView.updatePrograma(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
		} catch (Exception e) {
			data = null;
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_delete_datatable(ActionEvent evt) {
		try {
			selectedPrograma = (ProgramaDTO) (evt.getComponent()
					.getAttributes().get("selectedPrograma"));

			Integer idprograma = new Integer(selectedPrograma.getIdprograma());
			entity = businessDelegatorView.getPrograma(idprograma);
			action_delete();
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_delete_master() {
		try {
			Integer idprograma = FacesUtils.checkInteger(txtIdprograma);
			entity = businessDelegatorView.getPrograma(idprograma);
			action_delete();
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public void action_delete() throws Exception {
		try {
			businessDelegatorView.deletePrograma(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
			action_clear();
			data = null;
		} catch (Exception e) {
			throw e;
		}
	}

	public String action_closeDialog() {
		setShowDialog(false);
		action_clear();

		return "";
	}

	public String actionDeleteDataTableEditable(ActionEvent evt) {
		try {
			selectedPrograma = (ProgramaDTO) (evt.getComponent()
					.getAttributes().get("selectedPrograma"));

			Integer idprograma = new Integer(selectedPrograma.getIdprograma());
			entity = businessDelegatorView.getPrograma(idprograma);
			businessDelegatorView.deletePrograma(entity);
			data.remove(selectedPrograma);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
			action_clear();
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_modifyWitDTO(String estadoRegistro,
			Date fechaCreacion, Date fechaUltimaModificacion,
			Integer idprograma, String isbn, String nombrePrograma,
			String usuarioCreador, String usuarioUltimaModificacion,
			Integer idfacultad_Facultad) throws Exception {
		try {
			entity.setEstadoRegistro(FacesUtils.checkString(estadoRegistro));
			entity.setFechaCreacion(FacesUtils.checkDate(fechaCreacion));
			entity.setFechaUltimaModificacion(FacesUtils
					.checkDate(fechaUltimaModificacion));
			entity.setIsbn(FacesUtils.checkString(isbn));
			entity.setNombrePrograma(FacesUtils.checkString(nombrePrograma));
			entity.setUsuarioCreador(FacesUtils.checkString(usuarioCreador));
			entity.setUsuarioUltimaModificacion(FacesUtils
					.checkString(usuarioUltimaModificacion));
			businessDelegatorView.updatePrograma(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
		} catch (Exception e) {
			// renderManager.getOnDemandRenderer("ProgramaView").requestRender();
			FacesUtils.addErrorMessage(e.getMessage());
			throw e;
		}

		return "";
	}

	public InputText getTxtIsbn() {
		return txtIsbn;
	}

	public void setTxtIsbn(InputText txtIsbn) {
		this.txtIsbn = txtIsbn;
	}

	public InputText getTxtNombrePrograma() {
		return txtNombrePrograma;
	}

	public void setTxtNombrePrograma(InputText txtNombrePrograma) {
		this.txtNombrePrograma = txtNombrePrograma;
	}

	public InputText getTxtIdfacultad_Facultad() {
		return txtIdfacultad_Facultad;
	}

	public void setTxtIdfacultad_Facultad(InputText txtIdfacultad_Facultad) {
		this.txtIdfacultad_Facultad = txtIdfacultad_Facultad;
	}

	public InputText getTxtIdprograma() {
		return txtIdprograma;
	}

	public void setTxtIdprograma(InputText txtIdprograma) {
		this.txtIdprograma = txtIdprograma;
	}

	public List<ProgramaDTO> getData() {
		try {
			if (data == null) {
				data = businessDelegatorView.getDataPrograma();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return data;
	}

	public void setData(List<ProgramaDTO> programaDTO) {
		this.data = programaDTO;
	}

	public ProgramaDTO getSelectedPrograma() {
		return selectedPrograma;
	}

	public void setSelectedPrograma(ProgramaDTO programa) {
		this.selectedPrograma = programa;
	}

	public CommandButton getBtnSave() {
		return btnSave;
	}

	public void setBtnSave(CommandButton btnSave) {
		this.btnSave = btnSave;
	}

	public CommandButton getBtnModify() {
		return btnModify;
	}

	public void setBtnModify(CommandButton btnModify) {
		this.btnModify = btnModify;
	}

	public CommandButton getBtnDelete() {
		return btnDelete;
	}

	public void setBtnDelete(CommandButton btnDelete) {
		this.btnDelete = btnDelete;
	}

	public CommandButton getBtnClear() {
		return btnClear;
	}

	public void setBtnClear(CommandButton btnClear) {
		this.btnClear = btnClear;
	}

	public TimeZone getTimeZone() {
		return java.util.TimeZone.getDefault();
	}

	public IBusinessDelegatorView getBusinessDelegatorView() {
		return businessDelegatorView;
	}

	public void setBusinessDelegatorView(
			IBusinessDelegatorView businessDelegatorView) {
		this.businessDelegatorView = businessDelegatorView;
	}

	public boolean isShowDialog() {
		return showDialog;
	}

	public void setShowDialog(boolean showDialog) {
		this.showDialog = showDialog;
	}

	/*
	 * COMBO DE FACULTADES
	 */

	public SelectOneMenu getSelectOneMenuFacultad() {
		return selectOneMenuFacultad;
	}

	public void setSelectOneMenuFacultad(SelectOneMenu selectOneMenuFacultad) {
		this.selectOneMenuFacultad = selectOneMenuFacultad;
	}

	public List<Facultad> getLasFacultades() {
		if (lasFacultades == null) {
			try {
				lasFacultades = businessDelegatorView.getFacultad();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		return lasFacultades;
	}

	public void setLasFacultades(List<Facultad> lasFacultades) {
		this.lasFacultades = lasFacultades;
	}

	public List<SelectItem> getLasFacultadesItems() {
		if (lasFacultadesItems == null) {
			try {
				lasFacultadesItems = new ArrayList<SelectItem>();
				List<Facultad> lasFacultades = businessDelegatorView
						.getFacultad();
				for (Facultad facultad : lasFacultades) {
					SelectItem selectItem = new SelectItem(
							facultad.getIdfacultad(),
							facultad.getNombreFacultad());
					lasFacultadesItems.add(selectItem);
				}
			} catch (Exception e) {

			}
		}
		return lasFacultadesItems;
	}

	public void setLasFacultadesItems(List<SelectItem> lasFacultadesItems) {
		this.lasFacultadesItems = lasFacultadesItems;
	}

	public String getStringFacultad() {
		return stringFacultad;
	}

	public void setStringFacultad(String stringFacultad) {
		this.stringFacultad = stringFacultad;
	}

}
